/*
 * @author : TF-BJ-C064
 * @creation : 2014-6-26 下午4:32:56
 * @description : 
 *
 */

package com.action.park;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import net.sf.json.JSONObject;
import com.base.BaseAction;
import com.service.StatCumulativeUserService;
import com.tool.RegexTool;
import com.util.GUtil;
/**
 * 用户量统计
 * <p>Title: MyPayStatistics</p>
 * <p>Description: </p>
 * <p>Company: www.teleframe.cn</p> 
 * @author	chenhui
 * @date	2014-8-19
 */
public class StatCumulativeUserAction extends BaseAction {

	private Logger log = Logger.getLogger(StatCumulativeUserAction.class);
	private static String Tag = StatCumulativeUserAction.class.getName();

	StatCumulativeUserService service;//交易记录Service

	String stattype; // day, week, month, year
	String datetime; // "yyyy-MM-dd", "yyyy-MM-dd", "yyyy-MM", "yyyy"
	JSONObject json;

	@Override
	public String handle() throws Exception {
		this.setHeaderTitle("用户量统计");
		return GUtil.SUCCESS;
	}
	
	/**
	 * 查询
	 * <p>Title: query</p>
	 * <p>Description: 
	 * 		PC端网页查询会直接进入此处，手机端调用会通过handle()中转
	 * </p>
	 * @return
	 * @throws Exception
	 * @see com.base.BaseAction#query()
	 */
	@Override
	public String query() throws Exception {
		this.setHeaderTitle("用户量统计");
		log.info("StatCumulativeUserAction");
		boolean bPass = true;
		if(stattype==null){
			bPass = false;
			this.setMsg("类型错误"); 
		}
		if(datetime==null){
			bPass = false;
			this.setMsg("时间类型错误"); 
		}
		if(bPass){
			if(stattype.equals("month")){
				return this.queryByMonth();
			}else if(stattype.equals("year")){
				return this.queryByYear();
			}
			this.setMsg("参数错误  ");
		}

		PrintWriter out = response.getWriter();
		out.print( this.getMsg());
		out.close();

		return GUtil.NONE;
	}

	public String queryByMonth() throws Exception{
		datetime =datetime.substring(0,7);
		PrintWriter out = response.getWriter();
		if( !RegexTool.checkMonth(datetime) ){
			log.info("输入日期错误 datetime="+datetime);
			out.print("输入日期错误"+datetime);
			out.close();
			return GUtil.NONE;
		}
		
		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM");
		//String datetime = "2014-05";
		Date timelong = formatDate.parse(datetime);// for test it is the right time, otherwise throw ParseException
		
		Map<Double, Double> summanager = new HashMap<Double, Double>();
		Map<Double, Double> sumuserinfo = new HashMap<Double, Double>();
		json = new JSONObject();

		Double[][] datamonth = service.queryByMonth(summanager, datetime);
		Double[][] datamonths = service.queryByMonths(sumuserinfo, datetime);
		if(datamonth!=null && datamonths!=null){
			json.put("error", 0);
			json.put("msg", "操作成功");
			//check 
			if(datamonth.length!=2&&datamonths.length!=2){
				json.put("error", -2);
				log.error(Tag+" 系统错误 queryByMonth");
				json.put("msg", "系统错误 ");
			}else{
				json.put("sum", datamonth[0].length);
				json.put("sum1", datamonths[0].length);
			}
			json.put("datamonth", datamonth);
			json.put("datamonths", datamonths);
		}else{
			json.put("error", -1);
			json.put("msg", "操作失败 ");
		}
		out.print(json);
		out.close();
		
		return GUtil.NONE;
	}
	public String queryByYear() throws Exception{
		datetime =datetime.substring(0,4);
		PrintWriter out = response.getWriter();
		if( !RegexTool.checkYear(datetime) ){
			log.info("输入日期错误 datetime="+datetime);
			out.print("输入日期错误"+datetime);
			out.close();
			return GUtil.NONE;
		}
		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy");
		//String datetime = "2014";
		Date timelong = formatDate.parse(datetime);// for test it is the right time, otherwise throw ParseException
		
		Map<Double, Double> summanager = new HashMap<Double, Double>();
		Map<Double, Double> sumuserinfo = new HashMap<Double, Double>();
		json = new JSONObject();
		Double[][] datayear = service.queryByYear(summanager, datetime);
		Double[][] datayears = service.queryByYeasr(sumuserinfo, datetime);
		if(datayear!=null && datayears!=null){
			json.put("error", 0);
			json.put("msg", "操作成功");
			//check 
			if(datayear.length!=2&&datayears.length!=2){
				json.put("error", -2);
				log.error(Tag+" 系统错误 queryByMonth");
				json.put("msg", "系统错误 ");
			}else{
				json.put("sum", datayear[0].length);
				json.put("sum1", datayears[0].length);
			}
			json.put("datayear", datayear);
			json.put("datayears", datayears);
		}else{
			json.put("error", -1);
			json.put("msg", "操作失败 ");
		}
		out.print(json);
		out.close();
		
		return GUtil.NONE;
	}
	///blow not used

	@Override
	public String add() throws Exception {
		return GUtil.EDIT;
	}

	@Override
	public String update() throws Exception {
		return GUtil.EDIT;
	}

	@Override
	public String delete() throws Exception {
		return this.query();
	}

	@Override
	public String view() throws Exception {
		return GUtil.VIEW;
	}

	@Override
	public boolean validate(Object obj) throws Exception {
		return false;
	}

	public String getStattype() {
		return stattype;
	}

	public void setStattype(String stattype) {
		this.stattype = stattype;
	}

	public String getDatetime() {
		return datetime;
	}

	public void setDatetime(String datetime) {
		this.datetime = datetime;
	}

	public StatCumulativeUserService getService() {
		return service;
	}

	public void setService(StatCumulativeUserService service) {
		this.service = service;
	}

}
